package com.hvgroup.modules.sanic.web;

import com.hvgroup.modules.sanic.service.SSOAuthorizerService;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ZingK
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/sso")
@Api(tags = "单点登入授权")
public class SSOAuthorizerCpntroller {


    @Value("${oss.base_url}")
    private String baseUrl;
    @Value("${oss.client_id}")
    private String clientId;
    @Value("${oss.client_secret}")
    private String clientSecret;
    @Value("${oss.grant_type}")
    private String grantType;
    @Value("${oss.redirect_url}")
    private String redirectUrl;


    private final SSOAuthorizerService ssoAuthorizerService;
    @GetMapping("/constructOauthLink")
    public ResponseEntity<Object> constructOauthLink() {

        String url = String.format("%s?client_id=%s&&response_type=code&redirect_uri=%s",
                baseUrl, clientId, redirectUrl);
        return ResponseEntity.ok(url);
    }

    @GetMapping("/redirectOauthLink")
    public ResponseEntity<?>  redirectOauthLink() {
        String url = String.format("%s/authorize?client_id=%s&&response_type=code&redirect_uri=%s",
                baseUrl, clientId, redirectUrl);
        return ResponseEntity.status(HttpStatus.FOUND)
                .header("Location", url)
                .build();

    }

    @GetMapping("/profile")
    public ResponseEntity<Object> profile(String code) {
        return ssoAuthorizerService.getProfile(code);
    }

}
